package com.kucai.yifaqapp.ui.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.util.SpUtils;
import com.kucai.yifaqapp.widget.BaseLoadingDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2017/8/4.
 * Activity基类
 * 构造基本逻辑与添加常用方法
 * 未绑定presenter
 */

public abstract class BaseActivitySimple extends AppCompatActivity {
    protected static final int REQUEST_LOGIN = 9008;
    public List<Disposable> disposableList;
    public BaseLoadingDialog dialog;
    public int SkinId = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setContentView());
        getWindow().setBackgroundDrawable(null);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        getWindow().setNavigationBarColor(Color.GRAY);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        SharedPreferences sharedPreferences = getSharedPreferences("skinConfig", Context.MODE_PRIVATE);
        SkinId = sharedPreferences.getInt("skinId", 17771);
        ChangeSkin();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //TODO 移除功能
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
    }


    /**
     * 初始化控件
     */
    private void initView() {
        //Dialog初始化
        dialog = new BaseLoadingDialog(this, R.style.CustomDialog);
        //将id未back的控件添加返回点击事件
        View backView = findViewById(R.id.back);
        if (backView != null) {
            backView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BaseActivitySimple.this.onBackPressed();//当id为back控件点击时触发onBackPressed
                }
            });
        }
    }

    /**
     * 获取DisposableList
     *
     * @return disposableList
     */
    public List<Disposable> getDisposableList() {
        if (disposableList == null) disposableList = new ArrayList<>();
        return disposableList;
    }


    /**
     * @return 默认SharedPreferences
     */
    public SharedPreferences getSharedPreferences() {
        return getSharedPreferences(getString(R.string.sp_name), MODE_PRIVATE);
    }

    /**
     * 关闭软键盘
     *
     * @param view getWindowToken()
     */
    protected void closeKeyboard(@NonNull View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     * 设置contentView
     *
     * @return
     */
    protected abstract int setContentView();

    protected abstract void ChangeSkin();

    public void startLogin() {
        BaseActivitySimple.this.startActivity(
                new Intent(BaseActivitySimple.this, LoginActivity.class));
    }


    public void startrepeatLogin() {
        showDialog(R.string.dialog_titleNeedLogin,
                R.string.dialog_titleNeedLogin1,
                R.string.goLogin,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Info.user = null;
                        SpUtils.clearUser(BaseActivitySimple.this);
                        BaseActivitySimple.this.startActivityForResult(
                                new Intent(BaseActivitySimple.this, LoginActivity.class),
                                REQUEST_LOGIN);
                    }
                },
                R.string.cancel1, null, false);
    }


    /**
     * 显示提示信息
     */
    public void showDialog(@StringRes int title,
                           @StringRes int msg,
                           @StringRes int negative,
                           DialogInterface.OnClickListener negativeListener,
                           @StringRes int positive,
                           DialogInterface.OnClickListener positiveListener,
                           boolean cancelable) {
        new AlertDialog.Builder(this)
                .setTitle(title)
                .setMessage(msg)
//                .setNegativeButton(negative, negativeListener)
//                .setPositiveButton(positive, positiveListener)
                .setPositiveButton(negative, negativeListener)
                .setNegativeButton(positive, positiveListener)
                .setCancelable(cancelable)
                .show();
    }


}
